package org.zhxy.core.system.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.zhxy.common.aop.AutomaticLog;
import org.zhxy.common.config.ServiceFactory;
import org.zhxy.common.response.Result;
import org.zhxy.common.response.ResultCode;
import org.zhxy.entity.system.Permission;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    //@AutomaticLog("权限数据集合")
    @RequiresPermissions("api-permission-search")
    @RequestMapping(value = "/getPermissionList", method = RequestMethod.POST)
    public Result getPermissionTreeList(@RequestBody Map<String, Object> map) {
        try {
            List<Permission> all = ServiceFactory.permissionService.findAllByCasCader(map);
            return new Result(ResultCode.SUCCESS_NO_MESSAGE,all);
        }catch (Exception e){
            return Result.FAIL();
        }
    }

    @AutomaticLog("保存权限")
    @RequiresPermissions("api-permission-save")
    @RequestMapping(value = "/savePermission",method = RequestMethod.POST)
    public Result savePermission(@RequestBody Permission permission){
        return ServiceFactory.permissionService.savePermission(permission);
    }

    @AutomaticLog("修改权限")
    @RequiresPermissions("api-permission-update")
    @RequestMapping(value = "/updatePermission",method = RequestMethod.POST)
    public Result updatePermission(@RequestBody Permission permission){
        return ServiceFactory.permissionService.updatePermission(permission);
    }

    @AutomaticLog("删除权限")
    @RequiresPermissions("api-permission-remove")
    @RequestMapping(value = "/removePermission/{id}",method = RequestMethod.GET)
    public Result removePermission(@PathVariable("id")String id) {
        return ServiceFactory.permissionService.removePermission(id);
    }

    @RequestMapping(value = "/getPermissionsByRoleId/{roleId}",method = RequestMethod.GET)
    public Result getPermissionsByRoleId(@PathVariable("roleId")String roleId){
        try {
            List<Permission> permissions = ServiceFactory.permissionService.getPermissionsByRoleId(roleId);
            return new Result(ResultCode.SUCCESS_NO_MESSAGE,permissions);
        }catch (Exception e){
            return Result.FAIL();
        }
    }
}
